package com.sunday.authorization.mock.data.dto;

import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 权限工具类
 *
 * @author zsy
 * @since 2024/3/18
 */
@Slf4j
public class PermissionDTOTool {

    private static Collection<PermissionDTO> permissionDTOS;

    private static Map<String, PermissionDTO> cache;

    static {
        permissionDTOS = init();
        cache = permissionDTOS.stream().collect(Collectors.toMap(PermissionDTO::getUrl, Function.identity()));
    }

    private static Collection<PermissionDTO> init() {

        List<PermissionDTO> arr = new ArrayList<>();

        PermissionDTO permissionDTO = new PermissionDTO();
        permissionDTO.setId(1L);
        permissionDTO.setName("黑名单");
        permissionDTO.setUrl("/blacklist/classify");
        permissionDTO.setBit(0);
        permissionDTO.setCode(1L);
        permissionDTO.setType(2);
        permissionDTO.setParentId(0L);
//        permissionDTO.setCreateTime(LocalDateTime.now());
        arr.add(permissionDTO);

        PermissionDTO permissionDTO1 = new PermissionDTO();
        permissionDTO1.setId(2L);
        permissionDTO1.setName("黑名单-查询");
        permissionDTO1.setUrl("/api/blacklist/classify/list");
        permissionDTO1.setBit(0);
        permissionDTO1.setCode(1L << 1);
        permissionDTO1.setType(1);
        permissionDTO1.setParentId(1L);
//        permissionDTO1.setCreateTime(LocalDateTime.now());
        arr.add(permissionDTO1);

        PermissionDTO permissionDTO2 = new PermissionDTO();
        permissionDTO2.setId(3L);
        permissionDTO2.setName("黑名单-新增");
        permissionDTO2.setUrl("/api/blacklist/classify/save");
        permissionDTO2.setBit(0);
        permissionDTO2.setCode(1L << 2);
        permissionDTO2.setType(1);
        permissionDTO2.setParentId(1L);
//        permissionDTO2.setCreateTime(LocalDateTime.now());
        arr.add(permissionDTO2);

        PermissionDTO permissionDTO3 = new PermissionDTO();
        permissionDTO3.setId(4L);
        permissionDTO3.setName("黑名单-删除");
        permissionDTO3.setUrl("/api/blacklist/classify/remove");
        permissionDTO3.setBit(0);
        permissionDTO3.setCode(1L << 3);
        permissionDTO3.setType(1);
        permissionDTO3.setParentId(1L);
//        permissionDTO3.setCreateTime(LocalDateTime.now());
        arr.add(permissionDTO3);

        PermissionDTO permissionDTO4 = new PermissionDTO();
        permissionDTO4.setId(5L);
        permissionDTO4.setName("黑名单-修改");
        permissionDTO4.setUrl("/api/blacklist/classify/update");
        permissionDTO4.setBit(0);
        permissionDTO4.setCode(1L << 4);
        permissionDTO4.setType(1);
        permissionDTO4.setParentId(1L);
//        permissionDTO4.setCreateTime(LocalDateTime.now());
        arr.add(permissionDTO4);

        return arr;

    }

    public static Collection<PermissionDTO> authorization() {

        List<PermissionDTO> arr = new ArrayList<>();

        PermissionDTO permissionDTO = new PermissionDTO();
        permissionDTO.setId(1L);
        permissionDTO.setName("黑名单");
        permissionDTO.setUrl("/blacklist/classify");
        permissionDTO.setBit(0);
        permissionDTO.setCode(1L);
        permissionDTO.setType(2);
//        permissionDTO.setCreateTime(LocalDateTime.now());
        arr.add(permissionDTO);

        PermissionDTO permissionDTO1 = new PermissionDTO();
        permissionDTO1.setId(2L);
        permissionDTO1.setName("黑名单-查询");
        permissionDTO1.setUrl("/api/blacklist/classify/list");
        permissionDTO1.setBit(0);
        permissionDTO1.setCode(1L << 1);
        permissionDTO1.setType(1);
        permissionDTO1.setParentId(1L);
//        permissionDTO1.setCreateTime(LocalDateTime.now());
        arr.add(permissionDTO1);

        PermissionDTO permissionDTO2 = new PermissionDTO();
        permissionDTO2.setId(3L);
        permissionDTO2.setName("黑名单-新增");
        permissionDTO2.setUrl("/api/blacklist/classify/save");
        permissionDTO2.setBit(0);
        permissionDTO2.setCode(1L << 2);
        permissionDTO2.setType(1);
        permissionDTO2.setParentId(1L);
//        permissionDTO2.setCreateTime(LocalDateTime.now());
        arr.add(permissionDTO2);

        PermissionDTO permissionDTO3 = new PermissionDTO();
        permissionDTO3.setId(4L);
        permissionDTO3.setName("黑名单-删除");
        permissionDTO3.setUrl("/api/blacklist/classify/remove");
        permissionDTO3.setBit(0);
        permissionDTO3.setCode(1L << 3);
        permissionDTO3.setType(1);
        permissionDTO3.setParentId(1L);
//        permissionDTO3.setCreateTime(LocalDateTime.now());
        arr.add(permissionDTO3);

//        PermissionDTO permissionDTO4 = new PermissionDTO();
//        permissionDTO4.setId(5L);
//        permissionDTO4.setName("黑名单-修改");
//        permissionDTO4.setUrl("/api/blacklist/classify/update");
//        permissionDTO4.setBit(0);
//        permissionDTO4.setCode( 1L << 4 );
//        permissionDTO4.setType(1);
//        permissionDTO4.setParentId(1L);
////        permissionDTO4.setCreateTime(LocalDateTime.now());
//        arr.add(permissionDTO4);

        return arr;

    }

    public static long[] collect(Collection<PermissionDTO> permissionDTOS) {
        long[] sum = new long[1];
        for (PermissionDTO permission : permissionDTOS) {
            sum[permission.getBit()] |= permission.getCode();
        }
        return sum;
//        long[] sum = new long[2];
//        permissionDTOS
//                .stream()
////                .collect(Collectors.groupingBy(PermissionDTO::getBit, Collectors.counting()));
//                .collect(Collectors.groupingBy(PermissionDTO::getBit, ));

//                .map(PermissionDTO)
//                .reduce()
    }

    public static boolean check(String targetUrl, long[] permissionSum) {
        return Optional.ofNullable(cache.get(targetUrl))
                .map(permission -> (permissionSum[permission.getBit()] & permission.getCode()) != 0)
                .orElse(false);
    }

}
